#pragma once

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <unordered_map>
#include <string>
#include <sys/socket.h>
#include <unordered_map>

// 提供算法
#define SEQ ": "

static std::unordered_map<std::string, std::string> con_type_lib
    = {{"html", "application/xhtml+xml"},
        {"json", "application/json"}};

static std::string Content_type_transform(const std::string& type)
{
    if (con_type_lib.find(type) != con_type_lib.end())
        return con_type_lib[type];
    return "suffix not find commit";
}


class Util
{
public:
    // 版本二：只读取一行，在外部进行行汇总。
    static int Readline(size_t _socket, std::string& out)
    {
        char ch = 'x';
        // std::string tmp;
        int set = 0;
        while (set = recv(_socket, &ch, 1, 0))
        {
            if(set == 1)
                // std::cout << "读取到: " << ch << std::endl;
            if (set == -1)
                return -1;
            if (ch == '\r')
            {
                // std::cout << "读取到反斜杠r" << std::endl;
                recv(_socket, &ch, 1,MSG_PEEK);
                if (ch == '\n')
                {
                    ch = '\n';
                    out.push_back(ch);
                }
                continue;   
            }
            else if (ch == '\n')
            {
                return 0;
            }
            else
            {
                out.push_back(ch);
            }
        }
        return 0;
    }

    // 首部分割开
    static int  Stringcut(std::string& str,std::string& cut1, 
        std::string& cut2, const std::string& seq = SEQ)
    {
        cut1 = str.substr(0, str.find(seq));
        if(str.find(seq) == std::string::npos)
            return -1;
        cut2 = str.substr(str.find(seq) + seq.size());
        return 0;
    }
};
    



